<?php
class CategoryModel extends Model {

	function __construct()
		{
			parent::Model();
			$this->load->library(array('ImageLib', 'CategoryLib', 'treelib'));
		}

	public function action($type, $id='')
		{
			switch ($type) {
				case "add":
					$this->addCategory();
					break;
				case "edit":
				   $this->editCategory($id);
					break;
				case "delete":
					$this->deleteCategory($id);
					break;
				case "sort":
					$this->sortCategory();
					break;
				case "delVideo":
					$this->deleteVideo($id);
					break;
				}
		}

	private function addCategory()
		{
			$this->load->helper('translit');

			if ($this->input->post('slug')) {
				$slug = $this->input->post('slug',true);
			}else{
				$slug = translit($this->input->post('name',true));
			}

			$category = new Category_Photo();

			$category->name = $this->input->post('name',true);
			$category->description = $this->input->post('description',true);
			$category->parent_id = $this->input->post('parent_id',true);
			$category->level = $this->categorylib->level($this->input->post('parent_id',true));
			$category->orders = $this->categorylib->order($this->input->post('order',true));
			$thumb = $this->imagelib->upload('category', false, true, 104, 500);
			$category->thumb = $thumb['file_name'];
			$category->slug = $slug;
			$category->save();

			redirect('/admin/category/');
		}

	private function editCategory($id)
		{
			$this->load->helper('translit');

			$category = $this->getCategory($id);

			if ($this->input->post('slug')) {
				$slug = $this->input->post('slug',true);
			}else{
				$slug = translit($this->input->post('name',true));
			}

			$category->name = $this->input->post('name',true);
			$category->description = $this->input->post('description',true);
			$category->slug = $slug;

			if ($_FILES['userfile']['name'] != '') 
			{
				if (isset($category->thumb) && file_exists('res/img/category/'.$category->thumb)) {
					unlink('res/img/category/'.$category->thumb);
				}
				$icon = $this->imagelib->upload('category', false, true, 104, 500);
				$category->thumb = $icon['file_name'];
			}
			
			if ($_FILES['uservideo']['name'] != '') 
			{
				$video = $this->imagelib->uploadVideo('category');
				$category->video = $video['file_name'];
			}
			$category->save();
			redirect('/admin/category/edit/'.$category->id);
		}

	private function deleteCategory($id)
		{
			$category = $this->getCategory($id);
			if (isset($category->thumb) && file_exists('res/img/category/'.$category->thumb)) {
				unlink('res/img/category/'.$category->thumb);
			}
			$category->delete();
			echo "Категория ".$this->input->post('name',true)." удалена";
		}
	
	private function deleteVideo($id)
		{
			$category = $this->getCategory($id);
			if (isset($category->video) && file_exists('res/img/category/'.$category->video)) {
				unlink('res/img/category/'.$category->video);
			}
			$category->video = '';
			$category->save();
		}

	private function sortCategory()
		{
			$action 				= $this->input->post('action',true);
			$updateRecordsArray 	= $this->input->post('recordsArray',true);

			if ($action == "updateRecordsListings"){
				$listingCounter = 1;
				foreach ($updateRecordsArray as $id => $key) {
					$cat = Doctrine::getTable('Category_Photo')->find($key);
					$cat->orders = $listingCounter;
					$cat->save();
					$listingCounter = $listingCounter+1;
				}

				echo 'Изменения сохранены';
			}
		}

	function getCategory($id, $array = false)
		{
			$category = Doctrine::getTable('Category_Photo')->find($id);
			if($category !== false) {
				if ($array) {
					return $category->toArray(true);
				}else{
					return $category;
				}
			}else{
				show_404('page');
			}
		}
	
	function getSubCat($id, $array = false)
		{
			$category = Doctrine::getTable('Category_Photo')->findByParent_id($id);
			if($category !== false) {
				if ($array) {
					return $category->toArray(true);
				}else{
					return $category;
				}
			}else{
				show_404('page');
			}
		}

	function getTree($level = "0")
		{
			return $this->treelib->get($level, 'Category_Photo');
		}
}
?>